Skip to content

fix(builder): preserve whitespace between SELECT and UNION body in CREATE VIEW (#655)#657

Open
SAY-5 wants to merge 1 commit intophpmyadmin:masterfrom
SAY-5:fix/issue-655-create-view-union-whitespace
Open

fix(builder): preserve whitespace between SELECT and UNION body in CREATE VIEW (#655)#657
SAY-5 wants to merge 1 commit intophpmyadmin:masterfrom
SAY-5:fix/issue-655-create-view-union-whitespace

Conversation

@SAY-5
Copy link
Copy Markdown

@SAY-5 SAY-5 commented Apr 30, 2026

Summary

Fixes #655. CreateStatement::build() reconstructed CREATE VIEW ... AS SELECT ... UNION ... by concatenating $builtStatement = $this->select->build() directly with TokensList::buildFromArray($this->body). Neither side carried a separator, so the rebuilt SQL collided the SELECT's last token with the UNION:

... WHERE 3 = 3union all (...)

This was reported downstream in phpmyadmin/phpmyadmin#19692 (and earlier as #18515) — the syntax-invalid output broke phpMyAdmin's exports.

Change

src/Statements/CreateStatement.php — insert a single space when both sides are non-empty and neither already ends/begins with a space. The existing WITH ... AS ( ... ) SELECT ... path already produces a trailing space on $builtStatement, so the conditional avoids double-spacing it.

Test plan

  • vendor/bin/phpunit tests/Builder/ — 110/110 pass (including the existing testBuilderView which already exercised UNION on a different shape)
  • New testBuilderViewWithUnionPreservesWhitespace reproducing the issue's exact CREATE ALGORITHM=UNDEFINED ... AS select 1 AS \`a\` where 3 = 3 union all (...) input. Asserts:
    • rebuilt SQL does not contain 3union all (collision)
    • rebuilt SQL does contain 3 union all (case-normalised)
  • Existing WITH+UNION test (CREATE view view_name AS WITH aa(col1) AS (...) SELECT ...) unchanged — the conditional separator preserves its prior trailing-space output

…EATE VIEW (phpmyadmin#655)

Signed-off-by: SAY-5 <say.apm35@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing space before UNION when building CREATE VIEW

1 participant